package com.hogan.hoganproject.base.mvp;

import com.hogan.hoganproject.base.api.BaseHttpApi;
import com.hogan.hoganproject.base.utils.LogUtil;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import retrofit2.Retrofit;

/**
 * Author: chenhu
 * Time: 2020/8/31 9:59
 * Description do somethings
 */
public class BasePresenter<V extends IBaseControl.IBaseView> implements IBaseControl.IBasePresenter<V> {
    protected V mView;
    protected Retrofit mBaseRetrofit;

    public BasePresenter() {
        mBaseRetrofit = BaseHttpApi.getInstance().getRetrofit();
    }

    private CompositeDisposable mCompositeDisposable;

    @Override
    public void onAttack(V view) {
        this.mView = view;
    }

    @Override
    public void onDetach() {
        this.mView = null;
        onUnsubscribe();
    }

    protected void addDisposable(Disposable disposable) {
        if (mCompositeDisposable == null) {
            mCompositeDisposable = new CompositeDisposable();
        }
        mCompositeDisposable.add(disposable);
        LogUtil.v("addDisposable");
    }

    private void onUnsubscribe() {
        if (mCompositeDisposable != null && !mCompositeDisposable.isDisposed()) {
            LogUtil.v("onUnsubscribe");
            mCompositeDisposable.clear();
            mCompositeDisposable = null;
        }
    }
}
